package com.cskaoyan.service.wx_collect;

import com.cskaoyan.bean.BaseParam;
import com.cskaoyan.bean.PageDataVo;
import com.cskaoyan.bean.goods.Goods;
import com.cskaoyan.bean.promote.Topic;
import com.cskaoyan.bean.user.Collect;
import com.cskaoyan.bean.user.CollectExample;
import com.cskaoyan.bean.user.User;
import com.cskaoyan.bean.wx_collect.CollectGoodsVO;
import com.cskaoyan.mapper.goods.GoodsMapper;
import com.cskaoyan.mapper.promote.TopicMapper;
import com.cskaoyan.mapper.user.CollectMapper;
import com.cskaoyan.utils.Constant;
import com.cskaoyan.utils.MarketPageUtil;
import com.cskaoyan.utils.ReflectionSetJavaBeanUtils;
import com.github.pagehelper.PageHelper;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @description:
 * @author: enhui
 **/
@Service
public class CollectServiceImpl implements CollectService {
    @Autowired
    CollectMapper collectMapper;
    @Autowired
    GoodsMapper goodsMapper;
    @Autowired
    TopicMapper topicMapper;

    @Override
    public PageDataVo collectList(Integer type, BaseParam param) {
        //根据userId拿到收藏的商品的id
        User user = (User) SecurityUtils.getSubject().getPrincipals().getPrimaryPrincipal();
        CollectExample collectExample = new CollectExample();
        collectExample.setOrderByClause("update_time");
        CollectExample.Criteria collectExampleCriteria = collectExample.createCriteria();
        collectExampleCriteria.andUserIdEqualTo(user.getId()).andTypeEqualTo(type.byteValue()).andDeletedEqualTo(false);
        List<Collect> collects = collectMapper.selectByExample(collectExample);
        //封装到返回的collectVO中，包括收藏和商品或专题信息
        List<CollectGoodsVO> collectGoodsVOS = new ArrayList<>();
        for (Collect collect : collects) {
            Integer valueId = collect.getValueId();
            CollectGoodsVO collectGoodsVO = new CollectGoodsVO();
            collectGoodsVO.setId(collect.getId());
            collectGoodsVO.setType(type);
            if (type == 0) {
                //商品
                Goods goods = goodsMapper.selectByPrimaryKey(valueId);
                collectGoodsVO.setBrief(goods.getBrief());
                collectGoodsVO.setPicUrl(goods.getPicUrl());
                collectGoodsVO.setValueId(goods.getId());
                collectGoodsVO.setName(goods.getName());
                collectGoodsVO.setRetailPrice(goods.getRetailPrice());
            }
            if (type == 1) {
                //专题
                Topic topic = topicMapper.selectByPrimaryKey(valueId);
                collectGoodsVO.setBrief(topic.getSubtitle());
                collectGoodsVO.setPicUrl(topic.getPicUrl());
                collectGoodsVO.setValueId(topic.getId());
                collectGoodsVO.setName(topic.getTitle());
                collectGoodsVO.setRetailPrice(topic.getPrice());
            }
            collectGoodsVOS.add(collectGoodsVO);
        }
        PageDataVo pageDataVo = new PageDataVo();
        pageDataVo.setList(collectGoodsVOS);
        pageDataVo.setTotal(collectGoodsVOS.size());
        pageDataVo.setPages((int) (Math.floor(collectGoodsVOS.size() / param.getLimit()) + 1));
        pageDataVo.setLimit(param.getLimit());
        pageDataVo.setPage(param.getPage());
        return pageDataVo;
    }

    @Override
    public int addCollect(Integer type, Integer valueId) {
        User user = (User) SecurityUtils.getSubject().getPrincipals().getPrimaryPrincipal();
        //先查，根据userId、type和valueId是否收藏过
        CollectExample collectExample = new CollectExample();
        CollectExample.Criteria criteria = collectExample.createCriteria();
        criteria.andUserIdEqualTo(user.getId()).andTypeEqualTo(type.byteValue()).andValueIdEqualTo(valueId);
        List<Collect> collects = collectMapper.selectByExample(collectExample);
        int count = 0;
        if (collects.size() == 0) {
            //没有收藏过，直接新增
            Collect collect = new Collect();
            collect.setUserId(user.getId());
            collect.setValueId(valueId);
            collect.setType(type.byteValue());
            ReflectionSetJavaBeanUtils.setJavaBean(collect);
            count = collectMapper.insertSelective(collect);
        } else {
            Collect record = new Collect();
            record.setUpdateTime(new Date());
            if (collects.get(0).getDeleted().equals(true)) {
                //收藏过
                record.setDeleted(false);
            } else {
                //取消收藏
                record.setDeleted(true);
            }
            count = collectMapper.updateByExampleSelective(record, collectExample);
        }
        if (count == 1) {
            return Constant.SUCCESS;
        }
        return Constant.FAIL;
    }
}
